﻿using Common.Contract.Authentication.Record;
using Common.Contract.Customer.Record;
using Customer.Core.Data;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Customer.Provider
{
    public class ExportCustomerProvider : GenericProvider, IExportCustomerProvider
    {
        public List<CustomerFreedomRecord> RetrieveCustomerFreedom(out int totalRecord, UserRecord dao)
        {
            var data = repository.DbContext as CustomerEntities;
            var query = from x in data.CustomerFreedoms
                        join y in data.Customers on x.Cif equals y.Cif
                        select new CustomerFreedomRecord
                        {
                            ReportDate = x.ReportDate,
                            DaysInCurStatus = x.DaysInCurStatus,
                            Cif = x.Cif,
                            LegalId = y.LegalId,
                            StartCurStatus = x.StartCurStatus,
                            Status = x.Status
                        };
            totalRecord = query.Count();
            return query.ToList();
        }
        public List<CustomerNeedUpdateInfoRaw> RetrieveCustomerNeedToUpdateInfoRaw(out int totalRecord, UserRecord dao)
        {
            var data = repository.DbContext as CustomerEntities;
            var query = from x in data.CustomerNeedUpdateInfoRaws
                        join y in data.Customers on x.T24Cif equals y.Cif
                        join z in data.Branchs on y.BranchId equals z.BranchId
                        where z.Region.Contains("MBD")
                        select x;
            totalRecord = query.Count();
            return query.ToList();
        }
        public List<CustomerRecord> RetrieveCustomerNeedToUpdateInfo(out int totalRecord, UserRecord dao)
        {
            var data = repository.DbContext as CustomerEntities;
            var query = from x in data.CustomerNeedUpdateInfos
                        join y in data.Branchs on x.BranchId equals y.BranchId
                        where y.Region.Contains("MBD")
                        select new CustomerRecord
                        {
                            CIF = x.Cif,
                            NoOfInfoNeedToUpdate = x.NoInfoNeedUpdate
                        };
            totalRecord = query.Count();
            return query.ToList();
        }
        public List<LoanMatdt> RetrieveCustomerLoanMatdt(out int totalRecord, UserRecord dao)
        {
            var data = repository.DbContext as CustomerEntities;
            var query = from x in data.LoanMatdts
                        join y in data.Branchs on x.BranchId equals y.BranchId
                        where y.Region.Contains("MBD")
                        select x;
            totalRecord = query.Count();
            return query.ToList();
        }
        public List<TblAlertDepositMatdtRecord> RetrieveCustomerDepositMaturity(out int totalRecord, UserRecord Dao)
        {
            CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
            var data = repository.DbContext as CustomerEntities;
            var query = (from x in data.TblAlertDepositMatdts
                         join y in data.Customers on x.Cif equals y.Cif
                         join z in data.Branchs on y.BranchId equals z.BranchId
                         where z.Region.Contains("MBD")
                         select x).AsEnumerable().Select(x =>
                        new TblAlertDepositMatdtRecord
                        {
                            Cif = x.Cif,
                            Acctno = x.Acctno,
                            OpenningDate = x.OpenningDate,
                            Matdt = x.Matdt,
                            Term = x.Term,
                            BalQd = x.BalQd.ToString("#,###", cul.NumberFormat),
                        });
            totalRecord = query.Count();
            return query.ToList();
        }
        public List<AlertLimitMaturityRecord> RetrieveCustomerLimitMaturity(out int totalRecord, UserRecord Dao)
        {
            CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
            var data = repository.DbContext as CustomerEntities;
            var query = (from x in data.AlertLimits
                         join y in data.Customers on x.Cif equals y.Cif
                         join z in data.Branchs on y.BranchId equals z.BranchId
                         where z.Region.Contains("MBD")
                         select x).AsEnumerable().Select(x =>
                        new AlertLimitMaturityRecord
                        {
                            BusinessDate = x.BusinessDate,
                            ApprovalDate = x.ApprovalDate,
                            ExpiryDate = x.ExpiryDate,
                            Cif = x.Cif,
                            Id = x.Id,
                            InternalAmountQd = x.InternalAmountQd
                        });
            totalRecord = query.Count();
            return query.ToList();
        }

        public List<CustomerRecord> RetrieveListCustomerForExport(out int totalRecord, UserRecord Dao)
        {
            var data = repository.DbContext as CustomerEntities;
            var query = (from x in data.Customers
                         join y in data.Branchs on x.BranchId equals y.BranchId
                         where y.Region.Contains("MBD")
                         select x).AsEnumerable().Select(x => new CustomerRecord
                         {
                             CIF = x.Cif,
                             Name = x.CusName,
                             BranchId = x.BranchId,
                             Address = x.Address,
                             DateOpenCif = x.CusOpenDate.Value,
                             BirthIncorpDate = x.BirthIncorpDate,
                             LegalId = x.LegalId,
                             IndustryCode = x.VpbIndustry,
                             VpbIndustry = x.IndustryName,
                             DoanhThu = x.DoanhThu,
                             EmailAddress = x.EmailAddr,
                             SubSegment = x.Segment,
                             Dao = x.Dao,
                             CustTypeCode = x.CustType,
                             CustTypeName = x.CustTypeName,
                             VpbIndustryEn = x.IndustryNameEn,
                             DaoName = x.DaoName,
                             CusStatus = x.CusStatus,
                             BusinessDate = x.BusinessDate,
                             ModifiedDate = x.LastUpdateDate
                         });
            totalRecord = query.Count();
            return query.ToList();
        }
    }
}
